|
1
|
|
|
/* eslint-env browser, jquery */ |
|
2
|
|
|
|
|
3
|
|
|
// Displays message to user |
|
4
|
|
|
function showStatus(message) { |
|
5
|
|
|
const $status = document.getElementById('status'); |
|
6
|
|
|
$status.innerHTML = message; |
|
7
|
|
|
setTimeout(() => { |
|
8
|
|
|
$status.innerHTML = ''; |
|
9
|
|
|
}, 1750); |
|
10
|
|
|
} |
|
11
|
|
|
|
|
12
|
|
|
// Saves options to localStorage. |
|
13
|
|
|
function saveOptions() { |
|
14
|
|
|
const $useMaildrop = $('#use-maildrop'); |
|
15
|
|
|
const $address = $('#maildrop-address'); |
|
16
|
|
|
|
|
17
|
|
|
$address.val($address.val().replace(/^\s*/, '').replace(/\s*$/, '')); |
|
18
|
|
|
const address = $address.val(); |
|
19
|
|
|
const isAddressValid = address.match(/^[a-z.-_]+$/i); |
|
20
|
|
|
|
|
21
|
|
|
if (isAddressValid) { |
|
22
|
|
|
$address.removeClass('invalid'); |
|
23
|
|
|
localStorage.address = address; |
|
|
|
|
|
|
24
|
|
|
} else { |
|
25
|
|
|
$useMaildrop.prop('checked', false); |
|
26
|
|
|
$address.addClass('invalid'); |
|
27
|
|
|
localStorage.sender = 'app'; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
const isMailDropEnabled = $useMaildrop.prop('checked'); |
|
31
|
|
|
localStorage.sender = (isMailDropEnabled && isAddressValid) ? 'maildrop' : 'app'; |
|
32
|
|
|
|
|
33
|
|
|
// Update status to let user know options were saved. |
|
34
|
|
|
showStatus('Options saved.'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
// Restores select box state to saved value from localStorage. |
|
38
|
|
|
function restoreOptions() { |
|
39
|
|
|
document.getElementById('use-maildrop').checked = (localStorage.sender === 'maildrop'); |
|
|
|
|
|
|
40
|
|
|
document.getElementById('maildrop-address').value = localStorage.address || ''; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
function resetOptions() { |
|
44
|
|
|
document.getElementById('maildrop-address').value = ''; |
|
45
|
|
|
document.getElementById('use-maildrop').checked = false; |
|
46
|
|
|
saveOptions(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
document.addEventListener('DOMContentLoaded', restoreOptions); |
|
50
|
|
|
document.querySelector('#save').addEventListener('click', saveOptions); |
|
51
|
|
|
document.querySelector('#reset').addEventListener('click', resetOptions); |
|
52
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.